<template>
  <div class="AdendDialog">
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="执行时间" prop="jobCron">
            <el-select v-model="ruleForm.jobCron" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="任务描述" prop="jobDesc">
            <el-input
              type="textarea"
              autosize
              placeholder="请输入任务描述"
              v-model="ruleForm.jobDesc"
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="负责人" prop="author">
            <el-select v-model="ruleForm.author" placeholder="请选择">
              <el-option
                v-for="item in userArr"
                :key="item.username"
                :label="item.username"
                :value="item.username"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="报警邮件" prop="alarmEmail">
            <el-input
              class="email-input"
              type="text"
              placeholder="请输入邮件地址"
              v-model="ruleForm.alarmEmail"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="调度状态">
            <el-switch
              v-model="ruleForm.triggerStatus"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >保存</el-button
        >
        <el-button @click="resetForm('ruleForm')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    row: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      ruleForm: {
        jobCron: "",
        jobDesc: "",
        author: "",
        alarmEmail: "",
        triggerStatus: false,
      },
      rules: {
        jobCron: [
          {
            required: true,
            message: "请选择执行时间",
            trigger: "blur",
          },
        ],
        jobDesc: [
          {
            required: true,
            message: "请输入任务描述",
            trigger: "blur",
          },
        ],
        author: [
          {
            required: true,
            message: "请选择负责人",
            trigger: "blur",
          },
        ],
        alarmEmail: [
          {
            required: true,
            message: "请输入报警邮件",
            trigger: "blur",
          },
        ],
      },
      options: [
        {
          value: "早上9:30",
          label: "早上9:30",
        },
        {
          value: "中午12:00",
          label: "中午12:00",
        },
        {
          value: "下午17:00",
          label: "下午17:00",
        },
      ],
      userArr: [],
    };
  },
  mounted() {
    this.getSysUserList();
    this.ruleForm = { ...this.row };
    this.ruleForm = {
      ...this.ruleForm,
      triggerStatus: this.row.triggerStatus ? true : false,
    };
  },
  methods: {
    // 表单提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$server
            .emitTask({
              ...this.ruleForm,
              triggerStatus: this.ruleForm.triggerStatus ? 1 : 0,
            })
            .then((res) => {
              this.$message({
                type: "success",
                message: "修改成功!",
              });
              this.$emit("chang");
              this.$emit("getScheduPageList");
            });
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$emit("chang");
    },
    getSysUserList: function () {
      const params = {
        orgId: "",
        keyWord: "",
        page: 1,
        pageSize: 1000000,
      };
      this.$server.getSysUserList(params).then((res) => {
        console.log(res.page.list);
        this.userArr = res.page.list;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-textarea__inner {
  width: 80% !important;
}
.email-input {
  width: 80% !important;
}
</style>